import { createStore } from 'vuex'
import login from './login/login.js'
const state = {
  globalAudio: null, //全局播放器实例
  musicList: [
    { id: '2090627077', name: '一千公里', arname: '杨和苏' },
    { id: '1858696234', name: '国王与法老', arname: '杨和苏' },
  ], //音乐列表
  musicSrc: '', //音乐播放地址
  isPlayer: false, //音乐是否在播放
  currentTime: '00:00', //音乐当前播放时间 分秒
  currentTimes: 0, //音乐当前播放时间 秒
  duration: 0, //当前音乐总长度
  parvalue: 0, //当前播放音乐进度 0-100
  musicInfo: {}, //当前音乐信息
  musicid: 0, //当前音乐id
  PlayerListFlag: false, //播放列表是否显示
  PlayerStatus: 0, //歌曲播放顺序：0.列表循环，1.随机播放，2.单曲循环
  PlayerHistory: [],
}
const getters = {}
const actions = {}
const mutations = {
  //修改音乐id
  UPDATE_MUSICID(state, id) {
    state.musicid = id
  },
  //修改播放音乐列表是否显示
  CHANGE_PLAYLIST(state, flag) {
    state.PlayerListFlag = flag
  },
  //保存播放器实例
  SAVE_AUDIOREF(state, globalAudio) {
    state.globalAudio = globalAudio
  },
  //更新音乐播放地址
  UPDATE_MUSICSRC(state, musicsrc) {
    state.musicSrc = musicsrc
  },
  //切换音乐是否播放状态
  CHANGE_PLAYER(state, change) {
    state.isPlayer = change
  },
  //更新当前音乐总长度
  UPDATE_MUSICDURATION(state, num) {
    state.duration = num
  },
  //更新音乐播放当前时间
  UPDATE_MUSICCURRENTTIME(state, time) {
    state.currentTime = time
  },
  //更新音乐播放进度条进度 格式：分秒
  UPDATE_PARVALUE(state, par) {
    state.parvalue = par
  },
  //更新音乐播放器当前时间 格式：秒
  UPDATE_MUSICCURRENTTIMES(state, times) {
    state.currentTimes = times
  },
  //添加音乐进入历史列表
  ADD_MUSICINLIST(state, music) {
    const result = state.musicList.some((item) => item.id == music.id)
    if (!result) {
      state.musicList.push(music)
    }
  },
  //添加音乐播放信息
  ADD_MUSICINFO(state, musicinfo) {
    state.musicInfo = musicinfo
  },
  //按id移除列表音乐
  REMOVE_MUSICFORID(state, i) {
    state.musicList.splice(i, 1)
  },
  //初始化所有数据
  INIT_ALLDATA(state) {
    state.musicList = []
    ;(state.musicSrc = ''), //音乐播放地址
      (state.isPlayer = false), //音乐是否在播放
      (state.currentTime = '00:00'), //音乐当前播放时间 分秒
      (state.currentTimes = 0), //音乐当前播放时间 秒
      (state.duration = 0), //当前音乐总长度
      (state.parvalue = 0), //当前播放音乐进度 0-100
      (state.musicInfo = {}) //当前音乐信息
  },
  //修改音乐播放器播放规则 循环 随机 单曲
  UPDATE_PLAYERSTATUS(state, status) {
    state.PlayerStatus = status
  },
  //添加播放记录
  ADD_PLAYERHISTORY(state, id) {
    state.PlayerHistory.push(id)
  },
  //添加音乐进列表
  ADD_MUSICSINLIST(state, list) {
    state.musicList = state.musicList.concat(list)

    var result = []
    var obj = {}
    for (var i = 0; i < state.musicList.length; i++) {
      if (!obj[state.musicList[i].id]) {
        result.push(state.musicList[i])
        obj[state.musicList[i].id] = true
      }
    }
    state.musicList = result
  },
}
const store = createStore({
  state,
  getters,
  actions,
  mutations,
  modules: {
    login,
  },
})

export default store
